package com.leyou.item.controller;

import com.leyou.item.entity.SpecGroup;
import com.leyou.item.servcie.SpecGroupService;
import com.leyou.item.servcie.SpecParamService;
import com.leyou.pojo.dto.SpecGroupDto;
import com.leyou.pojo.dto.SpecParamDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/spec")
public class SpecController {

    @Autowired
    private SpecGroupService specGroupService;
    @Autowired
    private SpecParamService specParamService;

    /**
     * 根据分类id查询规格参数组
     * @param categoryId
     * @return
     */
    @GetMapping("/groups/of/category")
    public ResponseEntity<List<SpecGroupDto>>  querySpecGroupById(@RequestParam("id") Long categoryId){

        List<SpecGroupDto> list = specGroupService.querySpecGroupById(categoryId);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据品牌id查询规格组和规格参数
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<SpecGroupDto>> querySpecGroupAndParamById(@RequestParam("id") Long categoryId){
        List<SpecGroupDto> list =  specGroupService.querySpecGroupAndParamById(categoryId);
        return ResponseEntity.ok(list);
    }

    /**\
     * 根据条件查询规格参数
     * @param categoryId
     * @param groupId
     * @param searching
     * @return
     */
    @GetMapping("/params")
    public ResponseEntity<List<SpecParamDto>>  querySpecParam(
            @RequestParam(value = "categoryId",required = false) Long categoryId,
            @RequestParam(value = "groupId",required = false) Long groupId,
            @RequestParam(value = "searching",required = false) Boolean searching
    ){
        List<SpecParamDto> list =   specParamService.querySpecParam(categoryId,groupId,searching);
        return ResponseEntity.ok(list);
    }

    /**
     * 新增规格组
     * @param specGroupDto  json格式，需要requestBody接受
     * @return
     */
    @PostMapping("/group")
    public ResponseEntity<Void>  saveGroup(@RequestBody SpecGroupDto specGroupDto){
            specGroupService.saveGroup(specGroupDto);
            return ResponseEntity.status(201).build();
    }

    /**
     * 修改规格组
     * @param specGroupDto
     * @return
     */
    @PutMapping("/group")
    public ResponseEntity<Void>  updateGroup(@RequestBody SpecGroupDto specGroupDto){
        specGroupService.updateGroup(specGroupDto);
        return ResponseEntity.status(204).build();
    }

    /**
     * 修改规格参数
     * @param specParamDto
     * @return
     */
    @PutMapping("/param")
    public ResponseEntity<Void>  updateParam(@RequestBody SpecParamDto specParamDto){
            specParamService.updateParam(specParamDto);
            return ResponseEntity.status(204).build();
    }

    /**
     * 新增规格参数
     * @param specParamDto
     * @return
     */
    @PostMapping("/param")
    public ResponseEntity<Void>  saveParam(@RequestBody SpecParamDto specParamDto){
        specParamService.saveParam(specParamDto);
        return ResponseEntity.status(201).build();
    }
}
